package com.h3c.ptability.controller;

import com.eos.common.constant.StatusCodeEnum;
import com.eos.common.util.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.h3c.ptability.mapstruct.PtEnumMapstruct;
import com.h3c.ptability.cache.PtEnumCache;
import com.h3c.ptability.entity.PtEnum;
import com.h3c.ptability.service.IPtEnumService;
import com.h3c.ptability.dto.PtEnumDTO;
import com.h3c.ptability.utils.AppBusinessException;
import com.h3c.ptability.utils.ReturnCodeData;
import org.springframework.http.HttpStatus;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Copyright (C), 2022-2022, h3c
 * FileName: PtEnumController
 *
 * @Author: l22886
 * @Date: 2022-03-14
 * Description: pt_enum controller
 * Version: 1.0
 */
@Api(tags = " PtEnum 接口")
@CrossOrigin
@Slf4j
@RestController
@RequestMapping(value = "/ptEnum", produces = "application/json;charset=utf-8")
public class PtEnumController {

    @Resource
    private IPtEnumService ptEnumService;

    /**
     * @title: queryFuzzyListForPage
     * @description: 列表分页模糊查询
     * @params: [ paramsDTO, currentPage, pageSize ]
     * @return: org.springframework.http.ResponseEntity
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @ApiOperation(value = "列表分页查询", notes = "列表分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "PtEnumDTO", paramType = "query"),
            @ApiImplicitParam(name = "currentPage", value = "当前页", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "一页数据条数", required = true, dataType = "Integer", paramType = "query")
    })
    @RequestMapping(value = "/queryFuzzyListForPage", method = RequestMethod.GET)
    public Result queryFuzzyListForPage(@ModelAttribute PtEnumDTO paramsDTO,
                                        @RequestParam(name = "currentPage", required = true) Integer currentPage,
                                        @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        log.info("准备执行  列表分页模糊查询，传入的参数为：" + JSON.toJSONString(paramsDTO));
        //分页
        Page page = PageHelper.startPage(currentPage, pageSize);
        //执行
        List<PtEnumDTO> resultDTOList = ptEnumService.selectFuzzyByDto(paramsDTO);
        PageInfo<PtEnumDTO> pageInfo = page.toPageInfo();
        pageInfo.setList(resultDTOList);
        log.info("执行列表分页模糊查询完成，准备返回的结果为：" + JSON.toJSONString(pageInfo));
        return new Result(true, 20000, "列表分页模糊查询成功", pageInfo);
    }

    /**
     * @title: queryListForPage
     * @description: 列表分页精确查询
     * @params: [ paramsDTO, currentPage, pageSize ]
     * @return: org.springframework.http.ResponseEntity
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @ApiOperation(value = "列表分页查询", notes = "列表分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "PtEnumDTO", paramType = "query"),
            @ApiImplicitParam(name = "currentPage", value = "当前页", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "一页数据条数", required = true, dataType = "Integer", paramType = "query")
    })
    @RequestMapping(value = "/queryListForPage", method = RequestMethod.GET)
    public Result queryListForPage(@ModelAttribute PtEnumDTO paramsDTO,
                                   @RequestParam(name = "currentPage", required = true) Integer currentPage,
                                   @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        log.info("准备执行  列表分页查询，传入的参数为：" + JSON.toJSONString(paramsDTO));
        //分页
        Page page = PageHelper.startPage(currentPage, pageSize);
        //执行
        List<PtEnum> resultDTOList = ptEnumService.queryEnumList(paramsDTO);
        PageInfo<PtEnum> pageInfo = page.toPageInfo();
        pageInfo.setList(resultDTOList);
        log.info("执行列表分页查询完成，准备返回的结果为：" + JSON.toJSONString(pageInfo));
        return new Result(true, 20000, "列表分页查询成功", pageInfo);
    }


    /**
     * @title: save
     * @description: 数据插入
     * @params: [ paramsDTO ]
     * @return: org.springframework.http.ResponseEntity
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @ApiOperation(value = "数据插入", notes = "数据插入")
    @ApiImplicitParams({@ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "PtEnum", paramType = "body")})
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(@RequestBody PtEnumDTO paramsDTO) {
        log.info("准备执行 数据插入，传入的参数为：" + JSON.toJSONString(paramsDTO));
        //执行
        Integer result = ptEnumService.insertDto(paramsDTO);
        if (Objects.nonNull(result) && result > 0) {
            PtEnumCache.putEnumDataToRedis(PtEnumMapstruct.INSTANCE.dtoToPtEnum(paramsDTO));
        }
        log.info("执行 数据插入完成，准备返回的结果为：" + JSON.toJSONString(result));
        return new Result(true, 20000, "数据插入成功", result);
    }

    /**
     * @title: update
     * @description: 数据修改
     * @params: [ paramsDTO ]
     * @return: org.springframework.http.ResponseEntity
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @ApiOperation(value = "数据修改", notes = "数据修改")
    @ApiImplicitParams({@ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "PtEnum", paramType = "body")})
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result update(@RequestBody PtEnumDTO paramsDTO) {
        log.info("准备执行 数据修改，传入的参数为：" + JSON.toJSONString(paramsDTO));
        //执行
        Integer result = ptEnumService.updateDto(paramsDTO);
        if (Objects.nonNull(result) && result > 0) {
            PtEnumCache.putEnumDataToRedis(PtEnumMapstruct.INSTANCE.dtoToPtEnum(paramsDTO));
        }
        log.info("执行 数据修改完成，准备返回的结果为：" + JSON.toJSONString(result));
        return new Result(true, 20000, "数据修改成功", result);
    }

    /**
     * @title: delete
     * @description: 数据删除
     * @params: [ id ]
     * @return: org.springframework.http.ResponseEntity
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @ApiOperation(value = "数据删除", notes = "数据删除")
    @ApiImplicitParams({@ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "PtEnum", paramType = "body")})
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Result delete(@RequestBody List<PtEnumDTO> paramsDTOList) {
        log.info("准备执行 数据删除，传入的参数为：" + JSON.toJSONString(paramsDTOList));
        //执行
        Integer result = ptEnumService.deleteDto(paramsDTOList);
        log.info("执行 数据删除 完成，准备返回的结果为：" + JSON.toJSONString(result));
        return new Result(true, 20000, "数据删除", result);
    }

    @PostMapping("/queryEnum")
    public Result queryEnum(@RequestBody PtEnumDTO dto) {
        List<PtEnum> ptEnums = ptEnumService.queryEnumList(dto);
        return new Result(true, 20000, "查询成功", ptEnums);
    }

    //根据应用和枚举编码查询对应的枚举至
    @RequestMapping(value = "/getEnumByCode", method = RequestMethod.GET)
    public ResponseEntity<JSONObject> getEnumByCode(@ModelAttribute PtEnumDTO paramsDTO) {
        log.info("准备执行 数据查询，传入的参数为：" + JSON.toJSONString(paramsDTO));
        JSONObject data = new JSONObject();
        try {
            //执行
            List<PtEnumDTO> result = ptEnumService.getEnumByCode(paramsDTO);
            data.put("data", result);
            data.put("msg", "查询成功");
            data.put("code", ReturnCodeData.SUCCESS);
        } catch (Exception e) {
            log.error("执行 查询 异常，异常信息为：", e);
            data.put("msg", e.toString());
            data.put("code", ReturnCodeData.FAIL);
        }
        log.info("执行 查询 完成，准备返回的结果为：" + JSON.toJSONString(data));
        return new ResponseEntity<>(data, HttpStatus.ACCEPTED);
    }


    @ApiOperation(value = "枚举值导入", notes = "枚举值导入")
    @ApiImplicitParams({@ApiImplicitParam(name = "file", value = "导入数据文件", required = true, dataType = "MultipartFile", paramType = "body")})
//    @RequestMapping(value = "/importEnum", method = RequestMethod.POST)
    @PostMapping("importEnum")
    public ResponseEntity<JSONObject> importEnum(@RequestParam(value = "appId") String appId,
                                                 @RequestParam(value = "file") MultipartFile file) {
        ptEnumService.importEnum(file, appId);
        JSONObject data = new JSONObject();
        data.put("data", "");
        data.put("message", "导入枚举值成功");
        data.put("code", ReturnCodeData.SUCCESS);

        log.info("执行 导入枚举值，准备返回的结果为：" + JSON.toJSONString(data));
        return new ResponseEntity<>(data, HttpStatus.ACCEPTED);
    }


    @ApiOperation(value = "枚举值导出", notes = "枚举值导出")
    @RequestMapping(value = "/downloadData", method = RequestMethod.POST)
    public Result downloadData(@RequestBody PtEnumDTO paramsDTO) {
        try {
            File downLoadFile = ptEnumService.download(paramsDTO, "ENUM_IMPORT", "枚举值导出");
            FileInputStream inputStream = new FileInputStream(downLoadFile);
            byte[] buffer = new byte[(int) downLoadFile.length()];
            inputStream.read(buffer);
            inputStream.close();
            Map<String, String> result = new HashMap<>();
            result.put("file", Base64Utils.encodeToString(buffer));
            result.put("filename", downLoadFile.getName());
            return new Result(true, StatusCodeEnum.NORMAL, result);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AppBusinessException("导出文件异常");
        }
    }


    @ApiOperation(value = "根据appId查询应用所有枚举值接口", notes = "根据appId查询应用所有枚举值接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "SdUserDTO", paramType = "body")})
    @RequestMapping(value = "/getAllEnumByAppId", method = RequestMethod.POST)
    public ResponseEntity getAllEnumByAppId(@RequestBody PtEnumDTO paramsDTO) {
        Map<String, List<PtEnum>> allEnum = ptEnumService.getAllEnumByAppId(paramsDTO);
        JSONObject data = new JSONObject();
        data.put("data", allEnum);
        data.put("message", "根据appId查询应用所有枚举值接口成功");
        data.put("code", ReturnCodeData.SUCCESS);

        return new ResponseEntity<>(data, HttpStatus.ACCEPTED);
    }

}
